
package com.amaze.escape.util;

import com.amaze.escape.generators.ChunkGeneratorTerrain;
import com.amaze.escape.world.World;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

/**
 *
 * @author Amaze Team
 */
public class HeightMapFrame extends javax.swing.JFrame {

    World _parent;
    ChunkGeneratorTerrain _tGen;
    Image _heightMap;

    /** Creates new form HeightMapFrame
     * @param w 
     */
    public HeightMapFrame(World w) {
        _parent = w;
        _tGen = new ChunkGeneratorTerrain(w.getSeed());
        initComponents();
        updateHeightMap();
        setAlwaysOnTop(true);

        Timer t = new Timer();
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                updateHeightMap();
            }
        }, 0, 3000);
    }

    private void updateHeightMap() {
        _heightMap = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = _heightMap.getGraphics();
        Vector3f offset = _parent.getPlayer().getPosition();

        for (int x = 0; x < getWidth(); x++) {
            for (int z = 0; z < getHeight(); z++) {
                float height = _tGen.calcHeightMap(x + offset.x - getWidth() / 2, z + offset.z - getHeight() / 2);
                height = (float) Math.min(height, 1.0);
                height = (float) Math.max(height, 0.0);
                g.setColor(new Color(height, height, height));
                g.fillRect(x, z, 1, 1);
                g.setColor(Color.red);
                g.fillRect(getWidth() / 2, getHeight() / 2, 5, 5);
            }
        }

        repaint();
    }

    /**
     * 
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        g.clearRect(0, 0, getWidth(), getHeight());
        if (_heightMap != null) {
            g.drawImage(_heightMap, 0, 0, this);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Height map");
        setMinimumSize(new java.awt.Dimension(512, 512));
        setResizable(false);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        getContentPane().setLayout(null);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        Vector3f offset = _parent.getPlayer().getPosition();
        Vector2f newPosition = new Vector2f(offset.x + evt.getX() - getWidth() / 2, offset.z + evt.getY() - getHeight() / 2);
        float height = _tGen.calcHeightMap(newPosition.x, newPosition.y) * 128 + 16;

        _parent.getPlayer().setPosition(new Vector3f(newPosition.x, height, newPosition.y));
    }//GEN-LAST:event_formMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
